/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HouseBN;

import DataAccess.FindResult;
import DataAccess.HouseDA;
import entity.House;
import entity.SwhUser;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author Mr-Quick
 */
public class HouseService {

    EntityManager em;

    public HouseService() {
        String persistenceUnitName = "SweetHomePU";
        EntityManagerFactory factory = Persistence.createEntityManagerFactory(persistenceUnitName);
        em = factory.createEntityManager();
    }

    public House Gethousebyid(String Idhouse) {
        HouseDA hda = new HouseDA(em);
        FindResult<House> h = hda.findAbsolutely("idHouse", Idhouse);
        if (h.isEmpty()) {
            return null;
        } else {
            return h.get(0);
        }
    }

    public List<entity.House> search(Date datefrom,
            Date dateto,
            String homestyle,
            String typehouse,
            String city,
            String location,
            int pricefrom,
            int priceto) {
        HouseDA houseDA = new HouseDA(em);
        return houseDA.search(datefrom, dateto, homestyle, typehouse, city, location, pricefrom, priceto);
    }

    public String Updatehouse(House house, String idhouse) {
        try {
            HouseDA hda = new HouseDA(em);
            HouseService hs = new HouseService();
            em.getTransaction().begin();
            House h = em.find(House.class, idhouse);
            if (h != null) {
                h.setBathroom(house.getBathroom());
                h.setBedroom(house.getBedroom());
                h.setCity(house.getCity());
                h.setDateUp(house.getDateUp());
                h.setDeposit(house.getDeposit());
                h.setHomestyle(house.getHomestyle());
                h.setIntroduction(house.getIntroduction());
                h.setLinkmap(house.getLinkmap());
                h.setLivingroom(house.getLivingroom());
                h.setLocation(house.getLocation());
                h.setSquarefootageEarth(house.getSquarefootageEarth());
                h.setSquarefootageHouse(house.getSquarefootageHouse());
                h.setTittlehouse(house.getTittlehouse());
                h.setTypehouse(house.getTypehouse());
                h.setTypepayment(house.getTypepayment());
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public List<House> Getallhousebyiduser(SwhUser user) {
        try {
            HouseDA hda = new HouseDA(em);
            FindResult<House> h = hda.findAbsolutely("iDUser", user);
            return h;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public void deleteonehousebyidhouse(House house) {
        HouseDA hds = new HouseDA(em);
        em.getTransaction().begin();
        hds.remove(house);
        em.getTransaction().commit();

    }

    public List<House> getallsellermessage() {
        try {
            HouseDA hda = new HouseDA(em);
            List<House> list = hda.findAbsolutely("homestyle", "Seller");
            return list;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    public List<House> getallleasermessage() {
        try {
            HouseDA hda = new HouseDA(em);
            List<House> list = hda.findAbsolutely("homestyle", "Lease");
            return list;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
    
        public List<House> gethouseforindex() {
        try {
            HouseDA hda = new HouseDA(em);
            List<House> Hlist;
            Query q = em.createQuery("select h from House h");
            Hlist = q.getResultList();
            return Hlist;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}